const koa = require("koa");
const { koaBody } = require("koa-body");//解析
const cors = require("koa-cors");//跨域
const {AlipaySdk} = require('alipay-sdk');//支付
const router = require("./router");
// const setPro=require('./app/mock/index')
// setPro()
// 实例化koa
const app = new koa();
// 解析
app.use(
  koaBody({
    multipart: true,
  })
);
// 处理跨域
app.use(
  cors({
    origin: "*",
  })
);

// 路由
app.use(router);
// 普通公钥模式
module.exports =  alipaySdk = new AlipaySdk({
    appId: '9021000140636678',
    // keyType: 'PKCS1', // 默认值。请与生成的密钥格式保持一致，参考平台配置一节
    privateKey:'MIIEowIBAAKCAQEAmcf/0uM7NBg6qHyTZadh4Uazt2LOHG4p/ggcJifjjrnSt5IuxAumFgBQrp+bgZS8Iu0xjNjFXeA/BdhCi9v0InMlPFenMujKJJQPntfty8NJu8PmUXp6+y9YOZ3E4nnrfu5JQYlSUshv5eO4AXz5xjGVSkITliYdtt39epmwd5/pn1dSp936vniMSvLeBaD9Amx7azqzZYRVd3Yy1zgox+daY4jAkYh5p6sXVth2HUKs2nlnvt6jPCegxb7KZDqcHbQ90vPJb986Bl8GWLtjB5SDmB2gKS4CrKoHBummzle5aRMABuOKGZPL7fc0MBMP+195niWPFrtWvULYuzf3VQIDAQABAoIBACsZCRJSSqJT9ASKPUjs+xqGlRFdKISBP1WJclIjNbwnSe3WkzKwTRC3b+UwqAVN6Ngk/5EIo2+mzCLYM/Y8Y5RQDux1aE91PEm6ERKkU2maxoxP37mQCO1inGgxVh9eqeg94dvzH1ZCBFI6WrRYESxBcySHljOrZQJ9/m8gqXssr7V6CXhVRhfyfTwtd75K437b/riMoKpUwcp42mtWwcgHnEb5lT/tKHvgBvIPuDSaF4SehuT053x16sW8bqItweAZ9zqPkco8q/9Mp5qs4OfEH3+6X4zOexBFvnw/t7F29ljVTMH6p30+3aJ0Zk9Qvxijp+W3Tc6Sjx9beIZ3duECgYEAyHjiXqJ17lhdIfP3ZSNIMIODOI5Z4y+W7OeivhnK6dt8HaQZhlHN+v4QaYXTiQQDZh3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gox+daY4jAkYh5p6sXVth2HUKs2nlnvt6jPCegxb7KZDqcHbQ90vPJb986Bl8GWLtjB5SDmB2gKS4CrKoHBummzle5aRMABuOKGZPL7fc0MBMP+195niWPFrtWvULYuzf3VQIDAQAB' ,
    gateway:'https://openapi-sandbox.dl.alipaydev.com/gateway.do',//支付宝网关地址
  });
// 支付 
router.get('/pay' ,(ctx)=>{
   const {id,price}=ctx.request.query 
    const bizContent = {
        out_trade_no: id,
        product_code: "FAST_INSTANT_TRADE_PAY",
        subject: 'title',
        body:'body',
        total_amount: price
      }
    
      // 支付页面接口，返回 html 代码片段，内容为 Form 表单
      const result = alipaySdk.pageExec('alipay.trade.page.pay', {
        method: 'GET',
        bizContent,
        returnUrl: 'https://www.taobao.com'
      });
      
  ctx.body={ 
    code:200,
    message:"成功",
    data:result
  }
})
// 监听端口
app.listen(3000, () => {
  console.log("启动成功");
});
